//
//  @implementation BasePagedListViewController.swift
//  CreateChina
//
//  Created by 贾培军 on 2020/4/12.
//  Copyright © 2020 贾培军. All rights reserved.
//

import UIKit

open class SWBasePagedListViewController: SWBasePageViewController, UITableViewDelegate, UITableViewDataSource {
    
    public let identifier = "cell"
    public lazy var tableView:UITableView = {
        var style:UITableView.Style = .plain
        if !self.plain {
            style = .grouped
        }
        let t = UITableView.init(frame: view.bounds, style: style)
        return t
    }()

    public var plain = false
    
    open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView.init();
    }
    open func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView.init();
    }
    open func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1;
    }
    open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1;
    }
    
    open override func checkHasMoreWithCount(total: NSInteger) {
        tableView.es.stopLoadingMore()
        if total < dataFetcher!.pageNum {
            tableView.es.noticeNoMoreData()
        }
    }
    
    open override func endHeader(){
        tableView.es.stopPullToRefresh()
    }
    
    open override func endFooter() {
        tableView.es.stopLoadingMore()
    }
           
    open override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.delegate = self
        tableView.dataSource = self
//        tableView.contentInsetAdjustmentBehavior = .never
        contentView.addSubview(tableView)
        tableView.snp.makeConstraints({ (m) in
            m.edges.equalTo(UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0));
        })
        tableView.es.addPullToRefresh(animator: pullHeader) {
            [unowned self] in
            self.firstPageFetch()
        }
        tableView.es.addInfiniteScrolling(animator: pullFooter) {
            [unowned self] in
            self.morePageFetch()
        }
        // Do any additional setup after loading the view.
    }
    
    override func reloadAndReset() {
        tableView.reloadData()
        tableView.contentOffset = .zero
        
    }
    
    override func reload() {
        tableView.reloadData()
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
